package com.difsonapps.android.marker;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.InputStreamBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.util.Log;

public class ImageUploader {
	private static final String FILE_PART_NAME = "file";
	String url;
	String filePath;
	IRefresher<String> successListener;
	IRefresher<String> errorListener;
	
	
	
	public ImageUploader(String filePath) {
		this(AppConstant.uploadPhotoUrl, filePath, null, null);
	}



	public ImageUploader(String url, String filePath,IRefresher<String> successListener, IRefresher<String> errorListener) {
		super();
		this.url = url;
		this.filePath = filePath;
		this.successListener = successListener;
		this.errorListener = errorListener;
	}



	public void upload(){
		StringBuilder builder = new StringBuilder();
		try { 
			HttpClient httpclient = new DefaultHttpClient();
            httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
            HttpPost httppost = new HttpPost(url);
            File file = new File(filePath);
            Log.i("Upload url", url);
            MultipartEntity mpEntity = new MultipartEntity();
            ContentBody foto = new FileBody(file, "image/jpeg");
            mpEntity.addPart(FILE_PART_NAME, foto);
            httppost.setEntity(mpEntity);
            httpclient.execute(httppost);
            Log.i("Upload path", filePath);
            httpclient.getConnectionManager().shutdown();
	        /*HttpClient httpclient = new DefaultHttpClient();
	        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
	        HttpPost httppost = new HttpPost(url);
	        
	        Bitmap bmp = BitmapFactory.decodeFile(filePath);
	        ByteArrayOutputStream bos = new ByteArrayOutputStream();
	        bmp.compress(CompressFormat.JPEG, 70, bos);
	        InputStream in = new ByteArrayInputStream(bos.toByteArray());
	        //ContentBody foto = new InputStreamBody(in, "image/jpeg", "filename");
	        ContentBody foto = new InputStreamBody(in, "image/jpeg", "your_image_name.jpg");
	        
	        //File file = new File(miFoto);
	        //ContentBody foto = new FileBody(file, "image/jpeg");
	        
	        MultipartEntity mpEntity = new MultipartEntity();
	        mpEntity.addPart(FILE_PART_NAME, foto);
	        httppost.setEntity(mpEntity);
	        HttpResponse response = httpclient.execute(httppost);
	        httpclient.getConnectionManager().shutdown();
	        
	        StatusLine statusLine = response.getStatusLine();
    		int statusCode = statusLine.getStatusCode();
    		if(statusCode == 200){
    			HttpEntity entity = response.getEntity();
    			InputStream content = entity.getContent();
    			BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    			String line;
    			while((line = reader.readLine()) != null){
    				builder.append(line);
    			}
    			if(successListener!=null) successListener.refresh(builder.toString());
    		} else {
    			Log.e("Image uploader","Failedet JSON object");
    			if(errorListener!=null) errorListener.refresh("Error "+statusCode);
    		}*/
	    } catch (Exception e) {
	        e.printStackTrace();
	        //Log.e("Image uploader",e==null?"":e.getMessage());
	        if(errorListener!=null) errorListener.refresh("Exception "+(e==null?"":e.getMessage()));
	    }
	}

}
